package com.itheima.controller;

import com.itheima.domain.PageBean;
import com.itheima.domain.Student;
import com.itheima.service.StudentService;
import com.itheima.service.impl.StudentServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/student/*")
public class StudentServlet extends BaseServlet {
    private StudentService studentService = new StudentServiceImpl();

    private void add(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取页面的数据
        Map<String, String[]> map = req.getParameterMap();
        //创建接收的对象
        Student stu = new Student();
        //beanUtils工具来处理
        BeanUtils.populate(stu, map);
        studentService.add(stu);
        resp.sendRedirect(req.getContextPath() + "/student/findAll");
    }

    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int current = 1;
        int size = 5;
        String currentstr = req.getParameter("current");
        String sizestr = req.getParameter("size");
        if (currentstr != null) {
            current = Integer.parseInt(currentstr);

        }
        if (sizestr != null) {
            size = Integer.parseInt(sizestr);
        }
        List<Student> students = studentService.findPages(current, size);
        PageBean pageBean = new PageBean();
        int count = studentService.findCount();
        int total = count % size == 0 ? count / size : (count / size + 1);
        pageBean.setCurrent(current);
        pageBean.setSize(size);
        pageBean.setData(students);
        pageBean.setCount(count);
        pageBean.setTotal(total);
        req.setAttribute("pageBean", pageBean);
        req.getRequestDispatcher("/list.jsp").forward(req, resp);
    }
    public void findById(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int id = Integer.parseInt(req.getParameter("id"));
        Student student = studentService.findById(id);
        String [] address = {"广东","广西 ","湖南"};
        req.setAttribute("contact", student);
        req.setAttribute("addresses",address);
        req.getRequestDispatcher("/update.jsp").forward(req, resp);
    }
    public void update(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取页面的数据
        Map<String, String[]> map = req.getParameterMap();
        //创建接收的对象
        Student stu = new Student();
        //beanUtils工具来处理
        BeanUtils.populate(stu, map);
        studentService.update(stu);
        resp.sendRedirect(req.getContextPath() + "/student/findAll");
    }
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int id = Integer.parseInt(req.getParameter("id"));
        studentService.delete(id);
        resp.sendRedirect(req.getContextPath() + "/student/findAll");
    }
}

